package com.xmkj.carwash.mine.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.common.carbean.CarListBean;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.entity.bean.CarTypesBean;
import com.common.retrofit.methods.AddCarMethods;
import com.common.retrofit.methods.GetCarTypesMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.xmkj.carwash.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chen on 2018/4/1.
 */

public class AddCarInfoActivity extends BaseMvpActivity {
    public static final String CARINFO = "carinfo";
    public static final String TITLE = "title";
    @BindView(R.id.iv_next)
    ImageView mIvNext;
    @BindView(R.id.tv_car_type)
    TextView mTvCarType;
    @BindView(R.id.et_car_bank)
    EditText mEtCarBank;
    @BindView(R.id.et_model)
    EditText mEtModel;
    @BindView(R.id.et_car_number)
    EditText mEtCarNumber;
    @BindView(R.id.et_color)
    EditText mEtColor;
    @BindView(R.id.tv_intrue)
    TextView mTvIntrue;
    @BindView(R.id.rl_choose)
    RelativeLayout rl_choose;
    private List<String> siteTypeStrList = new ArrayList<>();
    private OptionsPickerView siteTypePicker;
    private String title;
    private CarListBean.ListsEntity bean;
    private CarTypesBean infoBean;
    private int curentCarTypeId;
    String carId = "" ;
    String typename ="";

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_car;
    }

    @Override
    protected void onViewCreated() {
        bean = (CarListBean.ListsEntity) getIntent().getSerializableExtra(CARINFO);
        if(EmptyUtils.isNotEmpty(bean)){
            mTvCarType.setText(bean.typename);
            mEtCarBank.setText(bean.brand);
            mEtModel.setText(bean.model);
            mEtCarNumber.setText(bean.carnum);
            mEtColor.setText(bean.color);
            carId = bean.id+"";
            typename = bean.typename;
        }
    }

    @Override
    protected void doLogicFunc() {
        getCarTypes();
        attachClickListener(rl_choose);
        attachClickListener(mTvIntrue);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==rl_choose.getId()){
            if (siteTypePicker != null) {
                siteTypePicker.show();
            }
        }else if(view.getId()==mTvIntrue.getId()){
            if(EmptyUtils.isEmpty(getEditTextStr(mEtCarBank))
                    ||EmptyUtils.isEmpty(getEditTextStr(mEtCarNumber))
                    ||EmptyUtils.isEmpty(getEditTextStr(mEtColor))
                    ||EmptyUtils.isEmpty(getEditTextStr(mEtModel))
                    ||curentCarTypeId==0
                    ){
                showToastMsg("请输入完整信息");
                return;
            }
            gotoAddCar();


        }
//
    }

    private void gotoAddCar() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();

                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.ADDCAR, true));
                onBackPressed();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        AddCarMethods.getInstance().addCar(subscriber,uid,hashid,curentCarTypeId,getEditTextStr(mEtCarBank),getEditTextStr(mEtModel)
        ,getEditTextStr(mEtCarNumber),getEditTextStr(mEtColor),carId,typename);
        rxManager.add(subscriber);
    }

    private void getCarTypes() {
        showProgressingDialog();
        CommonSubscriber<CarTypesBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                infoBean = (CarTypesBean) o;
                dismissProgressDialog();
                initSexOptionsPicker(infoBean);
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        GetCarTypesMethods.getInstance().CarTypeData(subscriber,uid,hashid,1,"");
        rxManager.add(subscriber);
    }
    private void initSexOptionsPicker(final CarTypesBean infoBean) {
        siteTypeStrList.clear();
        if(EmptyUtils.isNotEmpty(infoBean)&&infoBean.lists.size()!=0){
            for (int i = 0; i < infoBean.lists.size(); i++) {
                siteTypeStrList.add(   infoBean.lists.get(i).cname);
            }
        }


        siteTypePicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

                mTvCarType.setText(siteTypeStrList.get(options1));
                curentCarTypeId = infoBean.lists.get(options1).id;
                typename = siteTypeStrList.get(options1) ;
            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        siteTypePicker.setPicker(siteTypeStrList);
    }
    @Override
    protected void setNavigation() {
        title = getIntent().getStringExtra(TITLE);

        if(EmptyUtils.isNotEmpty(title)){
            setNavigationBack(title);

        }else {
            setNavigationBack("添加车辆信息");

        }
    }



    @Override
    public void bindKnife() {
        ButterKnife.bind(this);

    }
}
